package easy

func ImageSmoother(img [][]int) [][]int {
	m := len(img)
	n := len(img[0])
	result := make([][]int, m)
	for i := 0; i < m; i++ {
		result[i] = make([]int, n)
		for j := 0; j < n; j++ {
			left := max(0, i-1)
			right := min(m-1, i+1)
			top := max(0, j-1)
			bottom := min(n-1, j+1)
			count := 0
			sum := 0
			for x := left; x <= right; x++ {
				for y := top; y <= bottom; y++ {
					count++
					sum += img[x][y]
				}
			}
			result[i][j] = sum / count
		}
	}
	return result
}
